#include "filepreviewwidget.h"
#include "ui_filepreviewwidget.h"
#include "Sound/soundmanager.h"
#include <QDebug>
#include <QFileInfo>
#include "eventcenter.h"
#include "filemanager.h"
FilePreviewWidget::FilePreviewWidget(QWidget *parent) :
    QDockWidget(parent),
    ui(new Ui::FilePreviewWidget)
{
    ui->setupUi(this);
    InitMusicWidget();
    connect(&EventCenter::Instance(),&EventCenter::ClickFileItem,this,&FilePreviewWidget::ItemClicked);
    connect(&EventCenter::Instance(),&EventCenter::DoubleClickFileItem,this,&FilePreviewWidget::ItemDoubleClicked);
}

FilePreviewWidget::~FilePreviewWidget()
{
    delete ui;
    delete image;
}

void FilePreviewWidget::ShowImage()
{
    CloseMusicWidget();
    ui->image->show();
    image = new QImage(path);
    QPixmap pixmap = QPixmap::fromImage(*image);
    pixmap.scaled(ui->image->size(),Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
    ui->image->setPixmap(pixmap);
    ui->image->setScaledContents(true);
    float w = image->width();
    float h = image->height();
    imageRatio = w/h;
    ui->image->resize(w,h);
}

void FilePreviewWidget::MusicButtonSlot()
{
    if(SoundManager::Instance().GetPlayer(SoundManager::BGM)->state() == QMediaPlayer::State::PlayingState)
    {
        SoundManager::Instance().Pause(SoundManager::BGM);
        ui->playButton->setText("播放");
        ui->playButton->setIcon(QIcon(QPixmap(":/res/icon/run.png")));
    }
    else
    {
        SoundManager::Instance().PlayBGM(path);
        ui->playButton->setText("暂停");
        ui->playButton->setIcon(QIcon(QPixmap(":/res/icon/pause.png")));
    }
}

void FilePreviewWidget::ShowMusicWidget()
{
    this->setWidget(ui->musicWidget);
    ui->image->hide();
    ui->playButton->setText("播放");
    ui->playButton->setIcon(QIcon(QPixmap(":/res/icon/run.png")));
    ui->musicName->setText(QFileInfo(path).fileName());
}
void FilePreviewWidget::paintEvent(QPaintEvent *event)
{
    float h = this->height() - 80.0f;//防止遮挡标题栏
    float w = this->width();
    if(ui->image->width()>ui->image->height())
    {
        h = w/imageRatio;
    }else
    {
        w = h * imageRatio;
    }
    w = qMax(0.0f,w);
    h = qMax(0.0f,h);
    ui->image->resize(w,h);
    float x = this->width()/2 - w/2;
    float y =this->height()/2 - h/2;
    ui->image->move(x,y);
}

void FilePreviewWidget::CloseMusicWidget()
{
    SoundManager::Instance().GetPlayer(SoundManager::BGM)->stop();
    SoundManager::Instance().PlayBGM(path);
    ui->soundTime->setText("00:00");
    ui->musicSlider->setValue(0);
    SoundManager::Instance().GetPlayer(SoundManager::BGM)->stop();
    ui->musicWidget->hide();
}

void FilePreviewWidget::InitMusicWidget()
{
    float sliderMaxValue = 1000.0f;
    ui->musicSlider->setMaximum(sliderMaxValue);
    QMediaPlayer * player = SoundManager::Instance().GetPlayer(SoundManager::BGM);
    connect(player,&QMediaPlayer::positionChanged,ui->musicSlider,[=](){
        ui->musicSlider->setValue(sliderMaxValue*player->position()/player->duration());
        int remainTime = (player->duration()-player->position())/1000;//s
        int second = remainTime % 60, minute = remainTime / 60;
        QString _second = second >=10?QString::number(second):"0" + QString::number(second);
        QString _minute = minute >= 10?QString::number(minute):"0" + QString::number(minute);
        ui->soundTime->setText(_minute + ":" + _second);
    });
    connect(ui->musicSlider,&QSlider::sliderMoved,player,[=](){
        player->setPosition(player->duration()*ui->musicSlider->value()/ui->musicSlider->maximum());
    });
    connect(ui->playButton,&QPushButton::clicked,this,[=](){
        this->MusicButtonSlot();
    });
    ui->musicWidget->hide();
}

void FilePreviewWidget::ItemClicked(QListWidgetItem *item)
{
    QString name = item->text();
    path = FileManager::Instance().curPath + "/" + name;
    int fileType = FileManager::GetFileType(name);
    if(fileType == FileManager::IMAGE)
    {
        ShowImage();
    }else if(fileType == FileManager::SOUND)
    {
        CloseMusicWidget();
        ShowMusicWidget();
    }
}

void FilePreviewWidget::ItemDoubleClicked(QListWidgetItem *item)
{
    QString name = item->text();
    if(name == "上级目录" || QFileInfo(FileManager::Instance().curPath + "/" + name).isDir())
    {
        ui->image->hide();
        CloseMusicWidget();
    }
}
